
"""
    解析IP归属地
"""

from geolite2 import geolite2


def geo(ip):
    reader = geolite2.reader()
    country = reader.get(ip)
    try:
        country_name = country['country']['names']['zh-CN']
    except Exception as e:
        country_name = '-'
    try:
        subdivisions_name = country['subdivisions'][0]['names']['zh-CN']
    except Exception as e:
        subdivisions_name = '-'
    try:
        city_name = country['city']['names']['zh-CN']
    except Exception as e:
        city_name = '-'
    city = country_name + ' ' + subdivisions_name + ' ' + city_name
    geolite2.close()
    return city
